﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCMS.Model;

namespace SCMS.CoreBusinessLogic.FleetAcceories
{
    public class FleetAccesoriesService : IFleetAccesoriesService
    {
        public bool IsFleetAccesorySaved(FleetEquipment entity)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetEquipments.AddObject(entity);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsFleetAccesoryEdited(FleetEquipment entity)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetEquipments.Attach(entity);
                db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public FleetEquipment GetFleetAccesoryById(Guid accesoryId)
        {
            using (var db = new SCMSEntities())
            {
                return db.FleetEquipments.FirstOrDefault(p => p.Id == accesoryId);
            }
        }

        public List<FleetEquipment> GetFleetAccesories(Guid cpId)
        {
            using (var db = new SCMSEntities())
            {
                return db.FleetEquipments.Where(p => p.CountryProgrammeId == cpId).ToList();
            }
        }

        public bool IsFleetAccesoryDeleted(Guid accesoryId)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetEquipments.DeleteObject(db.FleetEquipments.FirstOrDefault(p => p.Id == accesoryId));
                return db.SaveChanges() > 0 ? true : false;
            }
        }
    }
}
